<?php
/*****************************************************************************
 *                                                                           *
 * Shop-Script PREMIUM                                                       *
 * Copyright (c) 2005 WebAsyst LLC. All rights reserved.                     *
 *                                                                           *
 *****************************************************************************/
?><?php
	if (!strcmp($sub, "custgroup")) //show registered customers list
	{
		if ( isset($_GET["delete"]) )
		{
			if (CONF_BACKEND_SAFEMODE) //this action is forbidden when SAFE MODE is ON
			{
				Redirect("admin.php?dpt=custord&sub=custgroup&safemode=yes");
			}

			DeleteCustGroup( $_GET["delete"] );
			Redirect("admin.php?dpt=custord&sub=custgroup");
		}

		if ( isset($_POST["save_custgroups"]) )
		{
			if (CONF_BACKEND_SAFEMODE) //this action is forbidden when SAFE MODE is ON
			{
				Redirect("admin.php?dpt=custord&sub=custgroup&safemode=yes");
			}

			// add new group
			if ( trim($_POST["new_custgroup_name"]) != "" )
			{
				AddCustGroup( 
					$_POST["new_custgroup_name"], $_POST["new_custgroup_discount"], 
					$_POST["new_sort_order"] );
			}

			// update groups
			$data = array();
			foreach( $_POST as $key => $val )
			{
				if ( strstr($key, "custgroup_name_") )
				{
					$key = str_replace("custgroup_name_","",$key);
					$data[$key]["custgroup_name"]=$val;
				}
				if ( strstr($key, "custgroup_discount_") )
				{
					$key = str_replace("custgroup_discount_","",$key);
					$data[$key]["custgroup_discount"]=$val;
				}
				if ( strstr($key, "sort_order_") )
				{
					$key = str_replace("sort_order_","",$key);
					$data[$key]["sort_order"]=$val;
				}
			}

			foreach( $data as $key => $val )
			{
				UpdateCustGroup(
					$key,  
					$val["custgroup_name"], 
					$val["custgroup_discount"], 
					$val["sort_order"] );
			}

			Redirect("admin.php?dpt=custord&sub=custgroup");

		}


		// get all groups
		$custgroups = GetAllCustGroups();

		$smarty->assign("custgroups", $custgroups);
		
		//set sub template
		$smarty->assign("admin_sub_dpt", "custord_custgroup.tpl.html");
	}

?>